﻿using SpaceNeedle.SDK.Windows.Platform.Commands;
using SpaceNeedle.SDK.Windows.Platform.Data;
using SpaceNeedle.x6473.WindowsPhone.Common;
using SpaceNeedle.x6473.WindowsPhone.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SpaceNeedle.x6473.WindowsPhone.ViewModels
{
    public class InAppSettingsViewModel : ViewModelBase
    {
        public InAppSettingsViewModel() : base()
        {
            this.LoadCommand = new RelayCommand(Load);
            this.SaveCommand = new RelayCommand(Save);
        }

        #region Properties
        public AppSettings Settings
        {
            get { return this.Get<AppSettings>(); }
            set { this.Set(value); }
        }
        #endregion

        #region Commands
        public ICommand LoadCommand { get; set; }

        protected async override void Load()
        {
            this.Settings = AppViewModel.Instance.Properties.Settings;

            if (this.Settings != null)
            {
                return;
            }

            await AppViewModel.Instance.RestoreAppState();

            this.Settings = AppViewModel.Instance.Properties.Settings;

            if (this.Settings != null)
            {
                return;
            }

            this.Settings = new AppSettings();

            Tracking.SendView(ViewsNames.InAppSettings);
        }

        public ICommand SaveCommand { get; set; }

        private async void Save()
        {
            AppViewModel.Instance.Properties.Settings = this.Settings;
            await AppViewModel.Instance.SaveAppState();
        }
        #endregion
    }
}